﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace Niknak.Services.TaskScheduler.UI
{
    public static class Utils
    {
        /// <summary>
        /// See http://stackoverflow.com/questions/3261451/using-a-bitmask-in-c-sharp
        /// </summary>
        public static class FlagsHelper
        {
            public static void Set<T>(ref T flags, T flag) where T : struct
            {
                int flagsValue = (int)(object)flags;
                int flagValue = (int)(object)flag;

                flags = (T)(object)(flagsValue | flagValue);
            }

            public static void Unset<T>(ref T flags, T flag) where T : struct
            {
                int flagsValue = (int)(object)flags;
                int flagValue = (int)(object)flag;

                flags = (T)(object)(flagsValue & (~flagValue));
            }
        }

        public static string GetEnumDescription(Enum value)
        {
            DescriptionAttribute[] attributes = value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];
            return (((attributes != null) && (attributes.Length > 0)) ? attributes[0].Description : value.ToString());
        }

        public static int CalculateNumberOfPages(int totalNumberOfItems, int pageSize)
        {
            var result = totalNumberOfItems % pageSize;
            if (result == 0)
                return totalNumberOfItems / pageSize;
            else
                return totalNumberOfItems / pageSize + 1;
        }

        public static string GetPrettyTimespan(TimeSpan value)
        {
            // returns: "# month(s)/week(s)/day(s)/hour(s)/minute(s)/second(s))"
            string prettyDate = value.ToString();

            if ((int)value.TotalDays > 365)
            {
                prettyDate = (int)value.TotalDays / 365 + " year" + ((int)value.TotalDays / 365 >= 2 ? "s" : "");
            }
            else if ((int)value.TotalDays > 30)
            {
                prettyDate = (int)value.TotalDays / 30 + " month" + ((int)value.TotalDays / 30 >= 2 ? "s" : "");
            }
            else if ((int)value.TotalDays > 7)
            {
                prettyDate = (int)value.TotalDays / 7 + " week" + ((int)value.TotalDays / 7 >= 2 ? "s" : "");
            }
            else if ((int)value.Days >= 1)
            {
                prettyDate = (int)value.Days + " day" + ((int)value.Days >= 2 ? "s" : "");
            }
            else if ((int)value.TotalHours >= 1)
            {
                prettyDate = (int)value.TotalHours + " hour" + ((int)value.TotalHours >= 2 ? "s" : "");
            }
            else if ((int)value.TotalMinutes >= 1)
            {
                prettyDate = (int)value.TotalMinutes + " minute" + ((int)value.TotalMinutes >= 2 ? "s" : "");
            }
            else
            {
                prettyDate = (int)value.TotalSeconds + " second" + ((int)value.TotalSeconds >= 2 ? "s" : "");
            }
            return prettyDate;
        }
    }
}
